package com.shycloud.mido.common.pay.alipaynew;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.SettleDetailInfo;
import com.alipay.api.domain.SettleInfo;
import com.alipay.api.request.AlipayMobilePublicQrcodeCreateRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayMobilePublicQrcodeCreateResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Xiaoxing Yu
 * @date 2020/9/22
 **/

public class AlipayWebUtil {

	private static Log logger = LogFactory.getLog(AlipayWebUtil.class);

	public final static String APPID = "2017072507889679";
	public final static String RSA_PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALQ0VNpgGwDrAor48vENcIvlkLUBagsAPemdIpokQHHoBU+RcciHmd5XSlJZcGDu4utDs4/JI5EYRXooocENOwBTorbiHq3IVb0BfywLcuvTZuZiv3/T8iqqrIC0OSkNwKi8fLFEHHVTIbzmhHqb23gSsjLuemY6agjuXlW5+T6xAgMBAAECgYBkVRK9d27xhuemis9w6QO10H5OWVg6gcr27DdczTxYb0+tqpRIiz9fbQiDayrHvZn4VXx/XQUBfxsH1eGhxAUJyxV7sB8wzVVykYioPlNwNv4MH8abtuXZGR6fTAxYX5uL6Q90nbxWxZCwpbHCaW/WItjYSgJ4cRUmKFYuGKziAQJBAPZ75Re1LI3m4bDJBBIux/W05VFtYiuJxCdPzil9uTJPoMKIwf+A5GsMSQ4mb9VGMa8RM4frfQMGA1k3fqYXFCECQQC7KV4qc9NaB88izjU6CAgmiBrb6Ug56zluA9NAgBgf8cnq0TOdXXOUxlkFYo1xoHtx6cknxoIWSTSnwyZAi9iRAkBvmoWOiIAV+FZ0X3Ayc3AitremeOqcYBKq4h2s97olJ5zYuGduswO53KQvfgPhA5l3dUhp4JyIA+78NH5p4cVBAkB8mKyMnHEOVXaK2DZM/L3YUPF4hxPDIxonaDqBjQUCdyeRwFWZJHZmi6kF0bcR9uFXU2jLpvZBOdjo+hyECdVRAkEAmVqA/ZnEQldw0ws6Mb+7n+kdO/PKiBMdwj1f0YAR9jX6oa94Sg2UHhNyAaDAlHncvzzLxy5EJQMPvC2wYE+UbA==";
	public final static String ALIPAY_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";
	public final static String URL = "https://openapi.alipay.com/gateway.do";

	public static void alipayWebPay() {
		try {
			AlipayClient alipayClient = new DefaultAlipayClient(
					"https://openapi.alipay.com/gateway.do",APPID,RSA_PRIVATE_KEY,
//					"json","GBK", ALIPAY_PUBLIC_KEY,"RSA2");
					"json","UTF-8", ALIPAY_PUBLIC_KEY,"RSA");

			AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();

			request.setReturnUrl("");
			request.setNotifyUrl("");

			String outTradeNo = String.valueOf(System.currentTimeMillis());

			AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();

			model.setOutTradeNo(outTradeNo);
			model.setTotalAmount("0.01");
			model.setSubject("测试订单test1");

			List<GoodsDetail> goodsDetailList = new ArrayList<>();

			GoodsDetail goodsDetail = new GoodsDetail();
			goodsDetail.setGoodsId("123");
			goodsDetail.setGoodsName("测试商品");
			goodsDetail.setQuantity(1L);
			goodsDetail.setPrice("0.01");
			goodsDetailList.add(goodsDetail);

			model.setGoodsDetail(goodsDetailList);

			SettleInfo settleInfo = new SettleInfo();

			List<SettleDetailInfo> settleDetailInfos = new ArrayList<>();
			SettleDetailInfo detailInfo = new SettleDetailInfo();

			detailInfo.setTransInType("loginName");
			detailInfo.setTransIn("cuijin@joymido.com");
			detailInfo.setAmount("0.01");

			settleDetailInfos.add(detailInfo);
			settleInfo.setSettleDetailInfos(settleDetailInfos);
			model.setSettleInfo(settleInfo);

			request.setBizModel(model);

//			request.setBizContent("{" +
//					"\"out_trade_no\":\""+ outTradeNo +"\"," +
//					"\"total_amount\":8.88," +
//					"\"subject\":\"测试订单test1\"," +
//					"        \"goods_id\":\"goods-test-001\"," +
//					"\"goods_name\":\"iphone XS MAX\"," +
//					"\"quantity\":1," +
//					"\"price\":10," +
//					"\"settle_info\":{" +
//					"        \"settle_detail_infos\":[{" +
//					"          \"trans_in_type\":\"cardAliasNo\"," +
//					"\"trans_in\":\"A0001\"," +
//					"\"amount\":0.1" +
//					"          }]," +
//					"    }," +
//					"  }");

			AlipayTradePrecreateResponse response = alipayClient.execute(request);

			if(response.isSuccess()){
				System.out.println("调用成功");
				System.out.println(JSONObject.toJSONString(response));
			} else {
				System.out.println("调用失败");
				System.out.println(JSONObject.toJSONString(response));
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		alipayWebPay();
	}

}
